<?php

namespace app\api\model;

use think\Config;
use think\Db;
use think\Model;


class Cart extends Model {

	/**
	 * @var string $error 错误信息
	 */
	public $error = '';

	// 表名
	protected $name = 'cart';

	// 自动写入时间戳字段
	protected $autoWriteTimestamp = 'int';

	// 定义时间戳字段名
	protected $createTime = 'createtime';
	protected $updateTime = 'updatetime';

	/**
	 * 添加购物车
	 *
	 * @param $userId
	 * @param $goodsId
	 * @param $goodsNum
	 * @param $goodsSpeId
	 *
	 * @return bool
	 */
	public function add( $userId, $goodsId, $goodsNum, $goodsSpeId ) {
		// 商品信息
		$goods = Goods::detail( $goodsId );
		// 商品sku信息
		$goods['goods_sku'] = $goods->getGoodsSkuById( $goodsSpeId );
		// 判断商品是否下架
		if ( $goods['status'] !== 1 ) {
			$this->setError( '很抱歉，该商品已下架' );
			return false;
		}
		$row = $this->where( ['user_id' => $userId, 'goods_id' => $goodsId, 'goods_spec_id' => $goodsSpeId] )->find();
		// 判断商品库存
		$cartGoodsNum = $goodsNum + ( !empty( $row ) ? $row['num'] : 0 );
		if ( $cartGoodsNum > $goods['goods_sku']['stock_num'] ) {
			$this->setError( '很抱歉，商品库存不足' );
			return false;
		}
		if ( $row ) {
			$row->save( ['num' => $cartGoodsNum] );
		} else {
			$this->save( ['user_id' => $userId, 'goods_id' => $goodsId, 'goods_spec_id' => $goodsSpeId, 'num' => $cartGoodsNum] );
		}
		return true;
	}

	/**
	 * 添加购物车-微信抢购项目
	 *
	 * @param $userId
	 * @param $goodsId
	 * @param $goodsNum
	 *
	 * @return bool
	 */
	public function goods_add( $userId, $goodsId, $goodsNum ) {
		// 商品信息
		$goods = Goods::detail( $goodsId );
		// 判断商品是否下架
		if ( $goods['status'] !== 1 ) {
			$this->setError( '很抱歉，该商品已下架' );
			return false;
		}
		$row = $this->where( ['user_id' => $userId, 'goods_id' => $goodsId] )->find();
		// 判断商品库存
		$cartGoodsNum = $goodsNum + ( !empty( $row ) ? $row['num'] : 0 );
		if ( $cartGoodsNum > $goods['goods_sku']['stock_num'] ) {
			$this->setError( '很抱歉，商品库存不足' );
			return false;
		}
		if ( $row ) {
			$row->save( ['num' => $cartGoodsNum] );
		} else {
			$this->save( ['user_id' => $userId, 'goods_id' => $goodsId, 'num' => $cartGoodsNum] );
		}
		return true;
	}

	/**
	 * 获取当前用户购物车商品总数量
	 *
	 * @param $userId
	 *
	 * @return float|int
	 */
	public function getTotalNum( $userId ) {
		return $this->where( ['user_id' => $userId] )->sum( 'num' );
	}

	/**
	 * 购物车列表
	 *
	 * @param $userId
	 */
	public function getList( $userId ) {

		$cartData = collection( $this->where( ['user_id' => $userId] )->select() )->toArray();

		// 商品列表
		$goodsList = [];
		$goodsIds  = array_unique( array_column( $cartData, 'goods_id' ) );
		foreach ( ( new Goods() )->getListByIds( $goodsIds ) as $goods ) {
			$goodsList[$goods['id']] = $goods;
		}
		// 购物车商品列表
		$cartList        = [];
		$amount_price    = 0;
		$total_num       = $this->where( ['user_id' => $userId] )->sum( 'num' );
		$isGifted        = 0;
		$isCanUseCoupon  = 1;
		$isCanUseVoucher = 1;
		$savePrice       = 0;
		$gift_price      = 0;
		foreach ( $cartData as $key => $cart ) {
			// 判断商品不存在则自动删除
			if ( !isset( $goodsList[$cart['goods_id']] ) ) {
				$this->where( ['id' => $cart['id']] )->delete();
				continue;
			}
			/* @var Goods $goods */
			$goods = $goodsList[$cart['goods_id']];
			// 商品sku信息
//			$goods['goods_spec_id'] = $cart['goods_spec_id'];
			// 商品sku不存在则自动删除
//			if ( !$goods['goods_sku'] = $goods->getGoodsSkuById( $cart['goods_spec_id'] ) ) {
//				$this->where( ['id' => $cart['id']] )->delete();
//				continue;
//			}
			$goods['show_error'] = 0;
			// 判断商品是否下架
			if ( $goods['status'] !== 1 ) {
				$goods['show_error']      = 1;
				$goods['show_error_text'] = '已下架';
				$this->setError( '很抱歉，商品 [' . $goods['name'] . '] 已下架' );
				continue;
			}
			// 判断商品库存
//			if ( $cart['num'] > $goods['goods_sku']['stock_num'] ) {
//				$goods['show_error']      = 2;
//				$goods['show_error_text'] = '库存不足';
//				$this->setError( '很抱歉，商品 [' . $goods['name'] . '] 库存不足' );
//				continue;
//			}
			$goods['cart_id']   = $cart['id'];
			$goods['is_gift']   = 0;
			$goods['total_num'] = $cart['num'];
			// 商品单价
//			$goods['goods_price'] = $goods['goods_sku']['goods_price'];
			$goods_price = $goods['goods_price'];
			// 商品总价
//			$goods['total_price'] = $total_price = $old_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
			$goods['total_price'] = bcmul( $goods_price, $cart['num'], 2 );
			$goods['save_price']  = 0;
//			if ( !empty( $goods['active'][0] ) ) {
//				$active = $goods['active'][0]->toArray();/*10=直接折扣,11=买满折,12=件数折,13=任选折,14第二件半价,20=直接特价,21=件数特价,22=时段特价,30=同种商品买赠,31=非同种商品买赠*/
//				if ( empty( $active['is_voucher'] ) ) {
//					$isCanUseVoucher = 0;
//				}
//				if ( empty( $active['is_coupon'] ) ) {
//					$isCanUseCoupon = 0;
//				}
//				switch ( $active['type'] ) {
//					case 10://直接折扣
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id']] )->find();
//						if ( $rule ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						}
//						break;
//					case 11://买满折
//						// 商品总价
//						$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						$rule                 = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $goods['total_price']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $total_price >= $rule['full_num'] ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						}
//						break;
//					case 12://件数折
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $goods['total_num'] >= $rule['full_num'] ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						}
//						break;
//					case 13://任选折
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $total_num]] )->order( 'full_num', 'desc' )->find();
//						if ( $total_num >= $rule['full_num'] ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						}
//						break;
//					case 14://第二件半价
//						$oldNum               = ceil( $goods['total_num'] / 2 );
//						$halfNum              = floor( $goods['total_num'] / 2 );
//						$goods['total_price'] = bcmul( $goods['goods_price'], $oldNum, 2 ) + bcmul( $goods['goods_price'] / 2, $halfNum, 2 );
//						break;
//					case 20://直接特价
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id']] )->find();
//						if ( $rule ) {
//							// 商品单价
//							$goods['goods_price'] = $rule['discount_num'];
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						}
//						break;
//					case 21://件数特价
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $goods['total_num'] >= $rule['full_num'] && $rule['full_num'] > 0 ) {
//							$oldNum    = ceil( $goods['total_num'] % $rule['full_num'] );
//							$activeNum = floor( $goods['total_num'] / $rule['full_num'] );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $rule['discount_num'], $activeNum, 2 ) + bcmul( $goods['goods_price'], $oldNum, 2 );
//						}
//						break;
//					case 22://时段特价
//						$time  = time();
//						$rules = Db::name( 'active_rule' )->where( ['active_id' => $active['id']] )->select();
//						foreach ( $rules as $rule ) {
//							$startTime = strtotime( date( 'Y-m-d ' . $rule['start_date'] ) );
//							$endTime   = strtotime( date( 'Y-m-d ' . $rule['end_date'] ) );
//							if ( $startTime <= $time && $time < $endTime ) {
//								// 商品单价
//								$goods['goods_price'] = $rule['discount_num'];
//								// 商品总价
//								$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//								break;
//							}
//						}
//						break;
//					case 30://同种商品买赠
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $cart['num'] >= $rule['full_num'] ) {
//							$activeNum            = floor( $goods['total_num'] / $rule['full_num'] );
//							$actives              = $goods->toArray();
//							$actives['is_gift']   = 1;
//							$actives['total_num'] = $activeNum * $rule['discount_num'];
//							// 商品单价
//							$actives['goods_price'] = 0;
//							// 商品总价
//							$actives['total_price'] = 0;
//							$gift_price             += $actives['total_num'] * $goods['goods_price'];
//						}
//						break;
//					case 31://非同种商品买赠
//						if ( $isGifted === 0 ) {
//							$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $total_num]] )->order( 'full_num', 'desc' )->find();
//							if ( $rule && $total_num >= $rule['full_num'] ) {
//								$activeNum = floor( $goods['total_num'] / $rule['full_num'] );
//								$actives   = ( new Goods() )->getListById( $rule['goods_id'] );
//								if ( !empty( $actives ) ) {
//									$shop_goods_spec = Db::name( 'shop_goods_spec' )->where( ['goods_id' => $rule['goods_id']] )->order( 'goods_spec_id', 'asc' )->find();
//									if ( $shop_goods_spec ) {
//										$actives['goods_sku']     = $actives->getGoodsSkuById( $shop_goods_spec['goods_spec_id'] );
//										$actives['goods_spec_id'] = $shop_goods_spec['goods_spec_id'];
//										$actives['is_gift']       = 1;
//										$actives['total_num']     = $activeNum * $rule['discount_num'];
//										// 商品单价
//										$actives['goods_price'] = 0;
//										// 商品总价
//										$actives['total_price'] = 0;
//										$isGifted               = 1;
//										$gift_price             += $actives['total_num'] * $shop_goods_spec['goods_price'];
//										$actives['save_price']  = $actives['total_num'] * $shop_goods_spec['goods_price'];
//										$goods['save_price']    = 0;
//										$actives                = $actives->toArray();
//									} else {
//										unset( $actives );
//									}
//								}
//							}
//						}
//
//						break;
//				};
//				$goods['save_price'] = $old_price - $total_price;
//				// 规格信息
//				$goods['specData'] = $goods['spec_type'] === '20' ? $goods->getManySpecData( $goods['spec_rel'], $goods['spec'] ) : null;
//				$savePrice         += $goods['save_price'];
//			}
//			$amount_price += $total_price;
			$cartList[]   = $goods->toArray();
			if ( !empty( $actives ) ) {
				// 规格信息
				$cartList[] = $actives;
				unset( $actives );
			}
		}
		$coupon_list = [];
//		if ( $isCanUseCoupon ) {
//			$model       = new UserCoupon();
//			$coupon_list = $model->where( ['user_id' => $userId, 'status' => 1, 'bottom_price' => ['elt', $amount_price]] )->select();
//		}
//		$voucher_list = [];
//		if ( $isCanUseVoucher ) {
//			$model        = new UserVoucher();
//			$voucher_list = $model->where( ['user_id' => $userId, 'status' => 1, 'bottom_price' => ['elt', $amount_price]] )->select();
//		}
		// 订单总运费金额
		$config = Config::get( 'site' );
		return [
			'goods_list'           => $cartList,
//			'order_total_num'      => $this->getTotalNum( $userId ),
//			'express_price'        => $config['shipping_amount'],
//			'free_shipping_amount' => $config['free_shipping_amount'],
//			'freight_description'  => $config['freight_description'],
//			'ticket_description'   => $config['ticket_description'],
//			'score_description'    => $config['score_description'],
//			'amount_price'         => round( $amount_price, 2 ),
//			'save_price'           => round( $savePrice + $gift_price, 2 ),
//			'coupon_list'          => $coupon_list,
//			'voucher_list'         => $voucher_list,
//			'ticket_num'           => count( $coupon_list ) + count( $voucher_list ),
//			'score_change_price'   => $config['score_change_price'],
//			'user_score'           => Db::name( 'user' )->where( ['id' => $userId] )->value( 'score' ),
			'has_error'            => $this->hasError(),
			'error_msg'            => $this->getError(),
		];
	}

	/**
	 * 购物车结算列表
	 *
	 * @param $ids
	 * @param $userId
	 * @param $cartData
	 */
	public function getListByIds( $ids, $userId, $cartData = null ) {

		if ( is_null( $cartData ) ) {
			$cartData = collection( $this->where( ['id' => ['in', $ids]] )->select() )->toArray();
		}
		// 商品列表
		$goodsList = [];
		$goodsIds  = array_unique( array_column( $cartData, 'goods_id' ) );
		foreach ( ( new Goods() )->getListByIds( $goodsIds ) as $goods ) {
			$goodsList[$goods['id']] = $goods;
		}
		// 购物车商品列表
		$cartList = [];
		//$amount_price = 0;//活动后商品金额
		//$totalPrice   = 0;//活动前商品金额
		if ( is_null( $cartData ) ) {
			$total_num = $this->where( ['id' => ['in', $ids]] )->sum( 'num' );
		} else {
			if ( isset( $cartData[0] ) ) {
				$total_num = $cartData[0]['num'];
			}
		}

		$isGifted        = 0;
		$isCanUseCoupon  = 1;
		$isCanUseVoucher = 1;
		$savePrice       = 0;
		$gift_price      = 0;
		$give_score      = 0;
		$cost_price      = 0;
		foreach ( $cartData as $key => $cart ) {
			// 判断商品不存在则自动删除
			if ( !isset( $goodsList[$cart['goods_id']] ) ) {
				$this->where( ['id' => $cart['id']] )->delete();
				continue;
			}
			/* @var Goods $goods */
			$goods = $goodsList[$cart['goods_id']];
			// 商品sku信息
			//$goods['goods_spec_id'] = $cart['goods_spec_id'];
			// 商品sku不存在则自动删除
//			if ( !$goods['goods_sku'] = $goods->getGoodsSkuById( $cart['goods_spec_id'] ) ) {
//				if ( is_null( $cartData ) ) {
//					$this->where( ['id' => $cart['id']] )->delete();
//				}
//				continue;
//			}
			$goods['show_error'] = 0;
			// 判断商品是否下架
			if ( $goods['status'] !== 1 ) {
				$goods['show_error']      = 1;
				$goods['show_error_text'] = '已下架';
				$this->setError( '很抱歉，商品 [' . $goods['name'] . '] 已下架' );
				continue;
			}
			// 判断商品库存
//			if ( $cart['num'] > $goods['goods_sku']['stock_num'] ) {
			if ( $cart['num'] > $goods['stock_num'] ) {
				$goods['show_error']      = 2;
				$goods['show_error_text'] = '库存不足';
				$this->setError( '很抱歉，商品 [' . $goods['name'] . '] 库存不足' );
				continue;
			}

			$goods['is_gift']    = 0;
			$goods['total_num']  = $cart['num'];  //要购买的一个商品的总数量
			$goods['save_price'] = 0;
			// 商品单价
//			$goods['goods_price'] = $goods['goods_sku']['goods_price'];
			$goods_price         = $goods['goods_price'];
//			$goods['cost_price'] = $goods['goods_sku']['cost_price'] * $cart['num'];
//			$cost_price          += $goods['cost_price'];
			// 商品总价
//			$goods['total_price'] = $total_price = $old_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
			$goods['total_price'] = bcmul( $goods_price, $cart['num'], 2 );
//			if ( !empty( $goods['active'][0] ) ) {
//				$active = $goods['active'][0]->toArray();/*10=直接折扣,11=买满折,12=件数折,13=任选折,14第二件半价,20=直接特价,21=件数特价,22=时段特价,30=同种商品买赠,31=非同种商品买赠*/
//				if ( empty( $active['is_voucher'] ) ) {
//					$isCanUseVoucher = 0;
//				}
//				if ( empty( $active['is_coupon'] ) ) {
//					$isCanUseCoupon = 0;
//				}
//				if ( $active['is_score'] ) {
//					$give_score += $goods['goods_sku']['score'] * $cart['num'];
//				}
//				switch ( $active['type'] ) {
//					case 10://直接折扣
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id']] )->find();
//						if ( $rule ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//							$goods['save_price']  = $old_price - $total_price;
//						}
//						break;
//					case 11://买满折
//						// 商品总价
//						$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//						$rule                 = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $goods['total_price']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $total_price >= $rule['full_num'] ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//							$goods['save_price']  = $old_price - $total_price;
//						}
//						break;
//					case 12://件数折
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $goods['total_num'] >= $rule['full_num'] ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//							$goods['save_price']  = $old_price - $total_price;
//						}
//						break;
//					case 13://任选折
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $total_num]] )->order( 'full_num', 'desc' )->find();
//						if ( $total_num >= $rule['full_num'] ) {
//							// 商品单价
//							$goods['goods_price'] = round( $goods['goods_sku']['goods_price'] * $rule['discount_num'] / 10, 2 );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//							$goods['save_price']  = $old_price - $total_price;
//						}
//						break;
//					case 14://第二件半价
//						$oldNum               = ceil( $goods['total_num'] / 2 );
//						$halfNum              = floor( $goods['total_num'] / 2 );
//						$goods['total_price'] = bcmul( $goods['goods_price'], $oldNum, 2 ) + bcmul( $goods['goods_price'] / 2, $halfNum, 2 );
//						break;
//					case 20://直接特价
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id']] )->find();
//						if ( $rule ) {
//							// 商品单价
//							$goods['goods_price'] = $rule['discount_num'];
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//							$goods['save_price']  = $old_price - $total_price;
//						}
//						break;
//					case 21://件数特价
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $goods['total_num'] >= $rule['full_num'] && $rule['full_num'] > 0 ) {
//							$oldNum    = ceil( $goods['total_num'] % $rule['full_num'] );
//							$activeNum = floor( $goods['total_num'] / $rule['full_num'] );
//							// 商品总价
//							$goods['total_price'] = $total_price = bcmul( $rule['discount_num'], $activeNum, 2 ) + bcmul( $goods['goods_price'], $oldNum, 2 );
//							$goods['save_price']  = $old_price - $total_price;
//						}
//						break;
//					case 22://时段特价
//						$time  = time();
//						$rules = Db::name( 'active_rule' )->where( ['active_id' => $active['id']] )->select();
//						foreach ( $rules as $rule ) {
//							$startTime = strtotime( date( 'Y-m-d ' . $rule['start_date'] ) );
//							$endTime   = strtotime( date( 'Y-m-d ' . $rule['end_date'] ) );
//							if ( $startTime <= $time && $time < $endTime ) {
//								// 商品单价
//								$goods['goods_price'] = $rule['discount_num'];
//								// 商品总价
//								$goods['total_price'] = $total_price = bcmul( $goods['goods_price'], $cart['num'], 2 );
//								$goods['save_price']  = $old_price - $total_price;
//								break;
//							}
//						}
//						break;
//					case 30://同种商品买赠
//						$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//						if ( $rule && $cart['num'] >= $rule['full_num'] ) {
//							$activeNum            = floor( $goods['total_num'] / $rule['full_num'] );
//							$actives              = $goods->toArray();
//							$actives['is_gift']   = 1;
//							$actives['total_num'] = $activeNum * $rule['discount_num'];
//							// 商品单价
//							$actives['goods_price'] = 0;
//							// 商品总价
//							$actives['total_price'] = 0;
//							$actives['cost_price']  = $actives['goods_sku']['cost_price'] * $actives['total_num'];
//							$cost_price             += $actives['cost_price'];
//							$gift_price             += $actives['total_num'] * $goods['goods_price'];
//							$goods['save_price']    = $actives['total_num'] * $goods['goods_price'];
//						}
//						break;
//					case 31://非同种商品买赠
//						if ( $isGifted === 0 ) {
//							$rule = Db::name( 'active_rule' )->where( ['active_id' => $active['id'], 'full_num' => ['elt', $cart['num']]] )->order( 'full_num', 'desc' )->find();
//							if ( $rule && $total_num >= $rule['full_num'] ) {
//								$activeNum = floor( $goods['total_num'] / $rule['full_num'] );
//								$actives   = ( new Goods() )->getListById( $rule['goods_id'] );
//								if ( !empty( $actives ) ) {
//									$shop_goods_spec = Db::name( 'shop_goods_spec' )->where( ['goods_id' => $rule['goods_id']] )->order( 'goods_spec_id', 'asc' )->find();
//									if ( $shop_goods_spec ) {
//										$actives['goods_sku']     = $actives->getGoodsSkuById( $shop_goods_spec['goods_spec_id'] );
//										$actives['goods_spec_id'] = $shop_goods_spec['goods_spec_id'];
//										$actives['is_gift']       = 1;
//										$actives['total_num']     = $activeNum * $rule['discount_num'];
//										// 商品单价
//										$actives['goods_price'] = 0;
//										// 商品总价
//										$actives['total_price'] = 0;
//										$isGifted               = 1;
//										$gift_price             += $actives['total_num'] * $shop_goods_spec['goods_price'];
//										$actives['save_price']  = $actives['total_num'] * $shop_goods_spec['goods_price'];
//										$actives['cost_price']  = $actives['total_num'] * $shop_goods_spec['cost_price'];
//										$cost_price             += $actives['cost_price'];
//										$goods['save_price']    = 0;
//										$actives                = $actives->toArray();
//									} else {
//										unset( $actives );
//									}
//								}
//							}
//						}
//
//						break;
//				}
//				$savePrice += $goods['save_price'];
//			} else {
//			$give_score += $goods['goods_sku']['score'] * $cart['num'];
//			}
//			$amount_price += $total_price;
//			$totalPrice   += $old_price;
			$cartList[]   = $goods->toArray();
			if ( isset( $actives ) ) {
				$cartList[] = $actives;
				unset( $actives );
			}
		}
		$coupon_list = [];
//		if ( $isCanUseCoupon ) {
//			$model       = new UserCoupon();
//			$coupon_list = $model->where( ['user_id' => $userId, 'status' => 1, 'bottom_price' => ['elt', $amount_price]] )->select();
//		}
//		$voucher_list = [];
//		if ( $isCanUseVoucher ) {
//			$model        = new UserVoucher();
//			$voucher_list = $model->where( ['user_id' => $userId, 'status' => 1, 'bottom_price' => ['elt', $amount_price]] )->select();
//		}
		// 订单总运费金额
		$config = Config::get( 'site' );
		return [
			'goods_list'   => $cartList,
//			'express_price'        => $amount_price < $config['free_shipping_amount'] ? $config['shipping_amount'] : 0,
//			'free_shipping_amount' => $config['free_shipping_amount'],
//			'freight_description'  => $config['freight_description'],
//			'ticket_description'   => $config['ticket_description'],
//			'score_description'    => $config['score_description'],
//			'total_price'  => round( $totalPrice, 2 ),
//			'amount_price' => round( $amount_price, 2 ),
//			'save_price'           => round( $savePrice, 2 ),
//			'give_score'           => round( $give_score, 2 ),
//			'cost_price'           => round( $cost_price, 2 ),
//			'coupon_list'          => $coupon_list,
//			'voucher_list'         => $voucher_list,
//			'ticket_num'           => count( $coupon_list ) + count( $voucher_list ),
//			'score_change_price'   => $config['score_change_price'],
//			'user_score'           => Db::name( 'user' )->where( ['id' => $userId] )->value( 'score' ),
			'has_error'    => $this->hasError(),
			'error_msg'    => $this->getError(),
		];
	}

	/**
	 * 设置错误信息
	 *
	 * @param $error
	 */
	private function setError( $error ) {
		empty( $this->error ) && $this->error = $error;
	}

	/**
	 * 是否存在错误
	 * @return bool
	 */
	private function hasError() {
		return !empty( $this->error );
	}

	/**
	 * 获取错误信息
	 * @return string
	 */
	public function getError() {
		return $this->error;
	}

}
